package com.gitee.devinx3.mlc.controller;

import com.gitee.devinx3.mlc.entity.License;
import com.gitee.devinx3.mlc.service.LicenseService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping(value = "v1/organizations/{organizationId}/licenses")
public class LicenseController {

    @Autowired
    private LicenseService licenseService;

    private Random random = new Random();

    @HystrixCommand(fallbackMethod = "buildFallbackLicenseList")
    @GetMapping("hystrix")
    public List<License> getHystrixLicenses(@PathVariable("organizationId") String organizationId) {
        try {
            if (random.nextInt() % 2 == 0) {
                Thread.sleep(1600);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return licenseService.getLicensesByOrg(organizationId);
    }

    @GetMapping
    public List<License> getLicenses(@PathVariable("organizationId") String organizationId) {
        return licenseService.getLicensesByOrg(organizationId);
    }

    private List<License> buildFallbackLicenseList(String organizationId) {
        return Stream.of(new License()
                .withId("0000000-00-00000")
                .withOrganizationId(organizationId)
                .withProductName("Sorry no licensing information currently available"))
                .collect(Collectors.toList());
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(LicenseController.class);

    @GetMapping("/{licenseId}")
    public License getLicenses(@PathVariable("organizationId") String organizationId,
                               @PathVariable("licenseId") String licenseId) {
        LOGGER.info("LicenseController.getLicenses:  organizationId is {} , licenseId is {}", organizationId, licenseId);
        return licenseService.getLicense(organizationId, licenseId, "");
    }

    @GetMapping("/{licenseId}/{clientType}")
    public License getLicensesWithClient(@PathVariable("organizationId") String organizationId,
                                         @PathVariable("licenseId") String licenseId,
                                         @PathVariable("clientType") String clientType) {
        return licenseService.getLicense(organizationId, licenseId, clientType);
    }

    @PutMapping("/{licenseId}")
    public void updateLicenses(@PathVariable("licenseId") String licenseId, @RequestBody License license) {
        licenseService.updateLicense(license);
    }

    @PostMapping
    public void saveLicenses(@RequestBody License license) {
        licenseService.saveLicense(license);
    }

    @DeleteMapping("/{licenseId}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteLicenses(@PathVariable("licenseId") String licenseId, @RequestBody License license) {
        licenseService.deleteLicense(license);
    }
}
